Methods and arrangements to correct for double rounding errors when rounding floating point numbers to nearest  away

ABSTRACT

Methods and arrangements to correct for double rounding errors when rounding floating point numbers to nearest away are described. Embodiments include transformations, code, state machines or other logic to perform a floating point operation on one or more floating point numbers of precision P 1  in base b, producing positive result res 0  of precision greater than precision P 1 ; rounding positive result res 0  to precision P 1  to the nearest away, producing positive result res 1 ; and rounding the result res1 to precision P 2  to the nearest away, where P 2  is narrower than P 1 , producing result res 2 . The embodiments may also include correcting res 2  for double rounding errors. The correcting may include determining that res1 is midway between two consecutive floating point numbers of precision P 2 , the larger being res 2 , determining that rounding res 0  to produce res1 involved rounding up, and decrementing the significand of res 2  to obtain the corrected result.

FIELD

The present invention is in the field of performing arithmetic on computing devices. More particularly, embodiments are in the field of correcting double rounding errors when rounding to nearest away.

BACKGROUND

The DRAFT Standard for Floating-Point Arithmetic IEEE P754 (Oct. 5, 2007) mandates standards for performing floating point operations on a set of numbers to produce a result in one precision and rounding the results down to another precision. Existing floating point implementations may produce an incorrect result, a double rounding error, if they perform the combined operation in two steps—the first operation followed by the rounding. Implementing the combined operation by starting anew may prove burdensome.

BRIEF DESCRIPTION OF THE DRAWINGS

Aspects of the invention will become apparent upon reading the following detailed description and upon reference to the accompanying drawings in which like references may indicate similar elements:

FIG. 1 is a diagram of an embodiment of a computer capable of correcting double rounding errors in floating point computations, where the rounding is to nearest-away;

FIG. 2 is a flowchart of an embodiment of a method to correct double-rounding errors in floating point arithmetic, where the rounding is to nearest-away;

FIG. 3 is a diagram of an embodiment of an apparatus to correct double-rounding errors in floating point arithmetic, where the rounding is to nearest-away; and

FIG. 4 is an embodiment of hardware to correct double-rounding errors in floating point arithmetic, where the rounding is to nearest-away.

DETAILED DESCRIPTION OF EMBODIMENTS

The following is a detailed description of embodiments of the invention depicted in the accompanying drawings. The embodiments are in such detail as to clearly communicate the invention. However, the amount of detail offered is not intended to limit the anticipated variations of embodiments; but on the contrary, the intention is to cover all modifications, equivalents, and alternatives falling within the spirit and scope of the present invention as defined by the appended claims.

Generally speaking, methods and arrangements to correct for double rounding errors when rounding floating point numbers to nearest away are contemplated. Embodiments include transformations, code, state machines or other logic to perform a floating point operation on one or more floating point numbers of precision P1 in base b, thereby producing positive result res0 of precision greater than P1. The embodiments may also include rounding positive result res0 to precision P1 to the nearest away, thereby producing positive result res1. The embodiments may also include rounding the result res1 to precision P2 to the nearest away, where P2 is narrower than P1, thereby producing result res2. The embodiments may also include correcting res2 for double rounding errors. The correcting may include determining that res1 is midway between two consecutive floating point numbers of precision P2, the larger being res2, determining that rounding res0 to produce res1 involved rounding up, and decrementing the significand of res2 to obtain the corrected result res2′.

Turning to FIG. 1, depicted is an embodiment of a computer 100 capable of correcting double rounding errors in floating point computations, where the rounding is to the nearest away. The floating point representation of a number may consist of a sign, a significand, and an exponent. The value of the number may equal:

(−1)^(S)×significand×base^(exponent)

For example, the base-10 number 1.325 may be rewritten or represented as (−1)⁰×1325×10⁻³. In this example, the sign, 0, indicates the number is positive. The significand is 1325 and the exponent is −3. The number is significant to four digits. On a computer, the most common base is base 2 (binary). Base 10 (decimal) is often used, base 16 is occasionally used, and other bases may be used. The size of a floating point representation of a number on a computer usually is a multiple of a word. A 32-bit floating point may be of the form:

TABLE 1 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 s exponent significand In the above table, the sign s occupies 1 bit, the exponent occupies 8 bits, and the significand occupies 23 bits. The number stored in the exponent field may not be the actual exponent. Instead, the actual exponent may be sum of the number stored in the representation of the number plus another summand, the bias. For example, if the stored exponent is 2 and the bias is +5, the actual exponent is +7. A floating point number whose representation uses 32 bits may be called single precision. Double precision floating point numbers may use 64 bits, 1 bit for the sign, 11 for the exponent, and 52 for the significand. IEEE standard 754 prescribes standards for floating point numbers. The IEEE 754 standard specifies four formats for floating point numbers, namely: single, single extended, double and double extended.

The formats may vary in the size of the significand. The more digits available for the significand, the more precision available for the representation of a number. For example, the (decimal) number 12.34 may be represented exactly with four decimal digits (length of significand=4). If 3 digits are used, the number must be represented as 12.3 or 12.4. If 2 digits are used, the number must be represented as 12 or 13. For simplicity, in this and following examples, base 10 will be used, and the number of digits of the significand will be much smaller than in a standard computer representation. These examples are for illustration and not limitation.

In the course of performing floating point arithmetic, it may be necessary to convert a floating point number to a number with fewer digits in the significand. Performing an arithmetic operation may produce a number with more digits in the significand than are available. For example, exactly adding the two two-digit numbers 10 and 0.02 produces the four digit number 10.02. In a two-digit representation, the result would have to be converted to 10 or 11. In addition, a floating point number may be converted to a number of lesser significance. For example, a series of multiplications may be performed in double-precision. The result may be converted to single precision. This process of converting a floating point number to a floating point number with fewer digits is called rounding.

There are a variety of methods for rounding. In rounding to precision P in nearest-away mode, a floating point number of greater precision is rounded to the nearest neighboring floating-point number of precision P, with ties (midpoints between two consecutive floating-point numbers of precision P) rounded to the floating-point number of larger magnitude of the two nearest floating-point numbers surrounding it (the number which is ‘farther away from zero’). For example, in converting a three-digit number to a two-digit number, 10.5 is rounded up to the number 11, and −11.5 is rounded down to −12. In “nearest even,” a floating point number is converted to the nearest even floating point number of the desired number of digits. In case of a tie, the floating point number is rounded up if the next-higher number is even and rounded down otherwise. In a binary representation, even means the least significant bit is zero. For example, in converting a three-digit number to a two-digit number, 10.5 is rounded down to the even number 10, and 11.5 is rounded up to the even number 12. In rounding toward + infinity, a floating point number is rounded to the higher of the two numbers it is between, regardless of value. For example, in converting three-digit numbers to two-digit numbers, 1.23 is rounded to 1.3 and −2.86 is rounded to −2.8. In rounding toward − infinity, a floating point number is rounded towards − infinity, regardless of value. For example, 1.23 is rounded to 1.2 and −2.86 is rounded to −2.9.

Double rounding errors in rounding to nearest-away mode may occur typically when the result res0 of a floating-point operation with operands of precision P1 has more significant digits than permitted by precision P1. The result is rounded correctly (in the IEEE 754R sense) first to a result res1 of precision P1, and then res1 is rounded again to a narrower precision P2. Sometimes, the result res2 does not represent the IEEE-correct result res2′ that would be obtained were the original result res0 rounded directly to precision P2. In such cases res2 differs from res2′ by one unit-in-the-last-place (ulp), and an error which occurs in such a situation is called a double rounding error. Only positive results will be considered here, as treatment of negative results is similar because rounding to nearest-away is symmetric with respect to zero. A double rounding error for rounding to nearest-away can only be upward (when the result res is too large by 1 ulp). For example, in going from 3 digits to 2:

10.0+0.490=10.49→10.5 (the arrow will designate rounding).

If 10.5 is now rounded to two digits, the result is 11. Rounding 10.49 to 2 digits directly produces 10. Thus, the double rounding produced a result, 11, which is one higher than directly rounding to the desired precision.

Correcting certain double rounding errors is mandated by a proposed IEEE draft. Provided decimal floating-point arithmetic is supported, the revision of the IEEE Standard 754-1985 for Floating-Point Arithmetic mandates that the floating-point operations for addition, subtraction, multiplication, division, square root, and fused multiply-add (a unitary operation to calculate a×b+c) have to be capable of rounding results correctly to any supported floating-point format, for operands in any (and possibly different) supported floating-point format. The draft of the revised standard IEEE 754, published in October 2007, is nearing approval and is expected to become the new standard for floating-point arithmetic in the beginning of 2008. Once IEEE 754R becomes the new IEEE floating-point standard, operations with mixed-format operands and results will be required to generate correctly rounded results. This will be a requirement for both binary and decimal floating-point operations, and for all rounding modes, including the rounding to nearest-away mode.

Computer 100 of FIG. 1 includes random access memory (RAM) 105, a processor 130 or CPU, non-volatile memory 140, a communications adapter 150, and an Input/Output (I/O) interface adapter 160 connected by system bus 185. Stored in RAM 105 is corrector 110 and operating system 120. Corrector 110 may comprise computer program instructions for correcting double rounding errors. The computer program instructions may be stored in a library. In some embodiments, the routines composed of the instructions may be called directly from an application. In other embodiments, the routines may be inserted by a compiler.

Operating system 120 may comprise UNIX™, Linux™, Microsoft Windows™, AIX™, IBM's i5/OS™, or other operating systems useful for correcting for double rounding errors when rounding floating point numbers to nearest away as will occur to those of skill in the art. Corrector 110 and operating system 120 (components of software) are shown in RAM 105 in FIG. 1, but many components of such software may be stored in non-volatile memory 140 also. Further, while the components of such are shown simultaneously present in RAM, in some other embodiments, only some of the components of RAM 105 may be present at any given time.

Processor 130 may execute the instructions of code running on computer 100. Processor 130 contains arithmetic logic unit (ALU) 135. ALU 135 may perform arithmetic operations such as addition and multiplication and logical operations such as AND and OR. ALU 135 contains floating point unit 138. Floating point unit 138 may perform arithmetic operations involving numbers in floating point representation. The operations may include standard two-argument operations such as addition, subtraction, multiplication, and division; may include one-argument operations such as taking the square root; and may include the three-argument operation fused multiply-add. The operations may include rounding. In some embodiments, corrector 110 may improve the functioning of floating point unit 138 by correcting for double-rounding errors and helping to put floating point unit 138 in compliance with the requirements of the proposed standard IEEE 754.

Non-volatile computer memory 140 constitutes memory that may retain its contents when the power of computer 100 is turned off. Non-volatile memory may be implemented as a hard disk drive 142, optical disk drive 144, electrically erasable programmable read-only memory space (EEPROM or Flash memory) 146, RAM drives (not shown), or as any other kind of computer memory as will occur to those of skill in the art.

Communications adapter 155 may implement the hardware level of data communications between computer 100 and other computers, such as other computers 155. The data communications may occur directly or through a network. I/O interface adapter 160 implements user-oriented I/O through, for example, software drivers and computer hardware for controlling output to display devices such as display device 165 and audio output device 170 as well as user input from user input device 175 and audio input device 180. User input device 175 may include both a keyboard and a mouse. Audio output 170 may include speakers or headphones and audio input device 180 may include a microphone or other device to capture sound.

The computer and components illustrated in FIG. 1 are for explanation, not for limitation. In some embodiments, embedded systems, PDAs, cell phones, BlackBerries® and other computing devices may correct for double rounding errors when rounding floating point numbers to nearest away. In several embodiments, a computing device may contain two or more processors. In various embodiments, a computing device may use point-to-point interconnects rather than buses to connect processors or to connect a processor and another element of the computing system. In many embodiments, modules to correct for double rounding errors when rounding floating point numbers to nearest away may be implemented in hardware, firmware, or in state machines or may form a component of an operating system.

Turning to FIG. 2, depicted is a flowchart 200 of an embodiment of a method to correct double-rounding errors in floating point arithmetic, where the rounding is to nearest-away. The method of flowchart 2 includes performing an arithmetic operation to precision P1 (element 210). The operation may be a unary, binary, or ternary operation. For example, two numbers may be multiplied or added in double precision. The operation may be performed in two stages. The first stage (element 220) produces an intermediate result, res0, which may be of greater precision than the desired precision. For example, 9.01+0.003→9.013. The intermediate result, 9.013 has four significant digits. In the second stage, the intermediate result res0 is rounded to res1, a number with the desired precision (element 225). For example, in a calculation to three significant figures, 9.013 may be rounded to 9.01. The result res1 is then rounded to a number of less precision, res2 (element 230). For example, 9.01 may be rounded to the two-digit number 9.0.

The method includes testing whether a double rounding error has occurred (element 240). This test and the correction of double rounding errors in element 250 is based on the observation that such errors may occur when the first rounding pulls up the result to a value that is a midpoint between two consecutive floating-point numbers of the specified precision P2 (as seen on the real axis), and therefore the second rounding causes an error of 1 ulp upward (this is for positive results; the error would be downward for negative results but as mentioned, only positive results are considered here).

Three logical rounding indicators for each of the two rounding operations (greater than P1 to P1 and then P1 to P2) can be used for testing whether a double rounding occurs (C notation will be used for logical operators, as well as for bitwise logical operators). The three indicators are: is_inexact_It_midpoint, is_inexact_gt_midpoint, and is_midpoint. is_inexact_It_midpoint indicates that a number to be rounded is nearer to the lesser of the two numbers it lies between; is_inexact_gt_midpoint indicates that the number is nearer to the greater of the two numbers; and is_midpoint indicates halfway between. A fourth indicator can be derived from the first three:

is_exact=!is_inexact_It_midpoint&&!is_inexact_gt_midpoint&&!is_midpoint

is_exact indicates that the number exactly equals a number of lower precision; i.e., that its low-order digits are zeroes. For example, suppose a three-digit number is to be rounded to a two-digit number. Then,

9.02 is is_inexact_It_midpoint. It is closer to 9.0 than to 9.1.

9.05 is is_midpoint. It is midway between 9.0 and 9.1.

9.08 is is_inexact_gt_midpoint. It is closer to 9.1 than to 9.0.

9.00 and 9.10 are is_exact. The first rounds exactly to 9.0 and the second rounds exactly to 9.1.

In pseudo-code (or C), the test for a double-rounding error can be expressed as follows (“0” identifies the first rounding and 1 identifies the second rounding):

// avoid a double rounding error for rounding to nearest-away

if (is_midpoint1 && (is_midpoint0 ∥ is_inexact_gt_midpoint0))

The C expression means that a double rounding error has occurred if the second rounding involved a number halfway between the two nearest lower-precision numbers and the first rounding produced a greater number. In C, && means AND and ∥ means OR. The numbers after the rounding indicators refer to the roundings to which they apply. Thus, is_midpoint0 refers to the rounding from res0 to res1, and is_midpoint1 refers to the rounding from res1 to res2. Consider the earlier example, where 10.0+0.490 were added and rounded to a two-digit number.

10.0+0.490=10.49 (res0)→10.5(res1)→11 (res2), where the arrows indicate rounding.

The number involved in the second rounding, 10.5, is midway between 10 and 11. Therefore, is_midpoint1 is true. In addition, the number involved in the first rounding, 10.49, satisfies is_inexact_gt_midpoint0. The number is past the midpoint between 10.4 and 10.5, and is not exactly equal to 10.5. Since one part of the OR clause is true, the clause as a whole is true.

If the test of element 240 if satisfied, the method also includes correcting the double rounding error. In flowchart 200, the correction includes decrementing (reducing by one in the least significant part), the significand of res2 (element 250). In the example, res2 is 11. Decrementing it produces 10. This is the result of performing the original addition and rounding directly to two digits. The result of the addition, 10.49, is closer to 10 than to 11.

In rare cases, the above correction may produce the wrong result. Elements 255 and 260 test and correct for these cases. In C, the test of element 255 and correction of element 260 may be expressed as:

{ if (significand == b{circumflex over ( )}(P2−1)−1) { // falls below the smallest P2-digit  significand  significand = b{circumflex over ( )}P2−1;  unbiased_exp−−; // decrease exponent by 1 }

In the above code, P2 refers to the number of digits, and b to the base, 10 in all of the examples. If after decrementing the significand of res2, it falls below the smallest P2-digit number (element 255), the significand is replaced by the highest P2-digit number and the exponent of res2 is reduced by one (element 260). Consider a slight variation of the above example:

9.90+0.0490=9.949 (res0)→9.95(res1)→10 (res2)

Here, P2=2, and the exponent is 0. Simply decrementing the significand of res2 by 1 produces 9, a one-digit number. The test of element 255, however, is satisfied.

9=10²⁻¹−1=10−1

Therefore, the correction of element 260 is made. The significand of res2 is set equal to 99=10²−1. In addition, the exponent is decremented to −1. The number represented is 99×10⁻¹, or 9.9, the correct result. If the test of element 255 fails, the method of flowchart 200 may end.

The execution of elements 265, 270, 275, and 280 may provide correct indicators for the two-step rounding process used to calculate res2. In effect, they provide the indicators that would result from directly rounding from res0 to res2. The correct indicators may be useful in case further rounding is needed, such as to represent res2 in a normalized form. For example, suppose a calculation produces 0.023×10⁻², but the largest negative exponent permitted in the floating point representation is −3. Then res2 must be rewritten as a one digit number×10³. The rewriting requires rounding 2.3 to a one digit number. These elements of flowchart 200 are carried out only when the test in element 240 fails; that is, does not disclose a double rounding error. In some embodiments, however, the rounding indicators may also be corrected when a double rounding error has been corrected.

Element 265 tests whether the is_midpoint1 label is correct. If res1 is at a midpoint, but res0 was rounded to produce res1, then res0 was not at a midpoint, and a different rounding indicator is applied to the two-step rounding process. For example, suppose

10.54→10.5→11.

Then, the overall rounding process from 10.54 to 11 is best described by is_inexact_gt_midpoint. The initial number, 10.54 is greater than the midpoint 10.5 of the two closest two-digit numbers, namely 10 and 11. If the test of element 265 is true, an appropriate rounding indicator is set to TRUE (element 270) and the method of flowchart 200 ends. In some embodiments, a similar correction to the rounding indicators may be made in the case of a double rounding error. In that case, the overall rounding process is best described by is_inexact_It_midpoint, and that rounding indicator is set to TRUE.

If the test of element 265 is false, the method of FIG. 2 includes checking whether res1 is exact but res0 is not (element 275). For example, suppose 10.02→10.0→10. While the second rounding, from 10.0 to 10, is exact, the overall rounding is not. If the test of element 275 is satisfied, the rounding indicator for res2 is modified (element 280). In the above example, the appropriate indicator is is_inexact_It_midpoint. 10.02 is less than halfway between 10 and 11, the closest two-digit numbers. If the test of element 275 fails, the method of flowchart 200 ends.

Elements 265 through 280 may be expressed in pseudo-code as follows:

if (is_midpoint1 && is_inexact_lt_midpoint0) {  // pulled down to a midpoint  is_midpoint1 = 0; / / element 270  is_inexact_gt_midpoint1 = 1; element 270  } else if (!is_midpoint1 && // element 275   !is_inexact_lt_midpoint1 && !is_inexact_gt_midpoint1) {  // if this rounding was exact the result may still be  // inexact because of the previous rounding  if (is_midpoint0 || is_inexact_gt_midpoint0) //These four lines   is_inexact_gt_midpoint1 = 1; // perform modifying of  if (is_inexact_lt_midpoint0) // element 280   is_inexact_lt_midpoint1 = 1;  }

Line 1 of the code performs the test of element 265. If res1 was at a midpoint, but res0 was not exactly equal to its rounded value res1, then the overall rounding process did not start at a midpoint. Further, a double rounding error did not occur, because element 265 is performed only if the test for a double rounding error in element 240 has failed. Therefore, res0 must have been rounded down to res1. Accordingly, in lines 3 and 4, the midpoint indicator is set to FALSE (in pseudo-code, a value of zero indicates false), and the is_inexact_gt_midpoint indicator is set to TRUE (in pseudo-code, a non-zero value indicates true.) Otherwise, as indicated by the “else if” clause of line 5, the code tests if the second rounding was exact; that is, that res1 coincides with a floating point number of the desired number of digits. A rounding is exact if the starting number was not (indicated by the “!” symbol) at a midpoint, was not inexact but more than halfway, and was not inexact but less than halfway. This test is part of the test of element 275.

If the test is satisfied, the last four lines of the above code perform the remaining element of the test of element 275 and the action of element 280. If res0 was midway or greater, then the rounding indicator is_inexact_gt_midpoint is set to TRUE, because the overall rounding was up, but not from a midpoint. For example, 9.999→10.0→10. 9.999 is greater than the midpoint of 10 and 11. Similarly, if res0 was less than midway, the overall rounding is down, and the is_inexact_It_midpoint indicator is set to TRUE in line 11.

There are several methods for determining the values of the rounding indicators used to carry out the method of flowchart 200. One method involves obtaining the values of the rounding digit and sticky bit, two variables derived from the exact result. The rounding digit is the digit next to, and of lower weight than the least-significant digit that fits in the significand of the specified precision before rounding) and the sticky bit s is the logical OR of all the remaining lower bits). In other words, the sticky bit s is 1 if any of the digits to the right of the rounding digit is non-zero, and is 0 if all of these digits are 0. For binary floating-point operations using rounding to nearest-away, the rounding digit is a bit. The three rounding indicators can be derived from r and s as follows (˜x represents the bitwise complement of x and ‘&’ stands for bitwise logical AND):

is_inexact_lt_midpoint=˜r & s

is_inexact_gt_midpoint=r & s

is_midpoint=r & ˜s

is_exact=˜r & ˜s

In binary, the rounding bit is either 0 or 1. 1 is halfway, since 1 is halfway between 0 and 2. The first line sets the is_inexact_It_midpoint to TRUE if and only if the rounding bit is 0, but there are non-zero less-significant bits. Otherwise, the indicator is set to FALSE. For example, in rounding 10001 to two bits, the indicator is TRUE. The rounding bit, the third bit, is zero, and a bit to the right (the last) is non-zero. Similarly, the second line provides that in rounding 101100 to two bits, the is_inexact_gt_midpoint is set to TRUE. The third bit, the rounding bit, is 1 (TRUE) and there are less significant bits. The third line sets the is_midpoint indicator TRUE if and only if the rounding bit is 1 and all subsequent bits are 0. The fourth line sets the is_exact to TRUE if and only if the rounding bit and all bits to the right are 0.

For decimal floating-point operations, r constitutes a digit rather than a bit. In this case:

is_inexact_lt_midpoint=((r==0&&s!=0)∥(1<=r&&r<=4))

is_inexact_gt_midpoint=((r==5&&s!=0)∥(r>5))

is_midpoint=((r==5) && (s==0))

is_exact=((r==0) && (s==0))

This code is similar to the other code, except that values from 0 to 9 for the digits must be considered.

FIG. 4 illustrates the block diagram of a possible hardware implementation 400 to correct double rounding errors. The hardware includes a calculator 410, a rounder 420, and a corrector 430. Calculator 410 receives 3 inputs in precision P1, indicated by the three arrows labeled “P1” to the left of the block. Calculator 410 performs the indicated operation, producing intermediate result res0 of precision greater than P1. Calculator 410 then rounds the result to produce result res1, along with r0 (the rounding digit) and s0 (the sticky bit) for the rounding from res0 to res1. Rounder 420 takes the result res1 from calculator 410 and rounds it to the first precision, producing res2. Rounder 420 also calculates the rounding digit r1 and sticky bit s1 for the rounding from res1 to res2.

Block 430 performs logic to correct for double rounding errors. It includes COMB LOGIC 440 and SUB1 LOGIC 450. COMB LOGIC 440 tests for a double rounding error. Using inputs r0 and s0 from the first rounding (sent through pins 415 and 418 respectively) and r1 and s1 from the second rounding (sent through pins 425 and 428, respectively), COMB LOGIC 440 generates

sub1=is_midpoint1&&(is_midpoint0∥is_inexact_gt_midpoint0)

For binary floating-point this becomes:

sub1=(r1&˜s1)&&(r0&˜s0∥r0& s0)=r1&˜s1& r0

For decimal floating-point:

sub1=((r1==5)&&(s1==0))&&(((r0==5)&&(s0==0))∥(((r0==5)&&(s0!=0))∥(res0>5)))

The SUB1 LOGIC block 450 applies a correction to the result res2 of the second rounding based on the correction signal sub1 if and only if a double rounding error has occurred. Otherwise res2′=res2 is already correct. The SUB1 LOGIC 450 also performs the correction of elements 250 and 255 of FIG. 2.

The logic above recognizes the double rounding error of the example at the bottom of page 4. Given r0=9, s0=0, r1=5, and s1=0. It follows that

sub1=1&&(0∥1)=1,

and thus the double rounding error upward is detected by COMB LOGIC 440 and corrected by SUB1 LOGIC 450.

FIG. 4 is for illustration and not limitation. In the embodiment of FIG. 4, three operands are used, e.g. for a fused multiply-add. However, most operations where this method can be applied (addition, subtraction, multiplication, division) have two operands, and the square root has only one. The rest of the block diagram is the same in all cases. Overflow and underflow cases (which can occur when rounding from a wider to a narrower format) are not covered explicitly here, but are straightforward to take care of.

Returning to FIG. 2, in many embodiments, a method of correcting for double rounding errors may calculate the above rounding indicators based upon comparing the values of rounding numbers by different methods. Further embodiments may execute the following operations:

-   -   (1) Calculate the result res1 of the first operation, rounded to         nearest-away to precision P1:

res1=(res0)_(RA,P1) (res0 is the exact, un-rounded, result of the first operation)

-   -   (2) Calculate the result res1_RN of the first operation, rounded         to nearest-even to precision P1:

res1_(—) RN=(res0)_(RN,P1)

-   -   (3) Calculate the result res1_RM of the first operation, rounded         to negative infinity to precision P1:

res1_(—) RM=(res0)_(RM,P1)

-   -   (4) Calculate the result res1_RP of the first operation, rounded         to positive infinity to precision P1:

res1_(—) RP=(res0)_(RP,P1)

-   -   (5) Calculate (2*res0)_(RM,P1) (multiplication by 2 of one or of         all the operands of the original operation is exact in both base         2 and base 10)     -   (6) Calculate (2*res0)_(RP,P1)     -   (7) is_exact0=(res1_RM==res1_RP)     -   (8) is_midpoint0=!is_exact0 &&         ((2*res0)_(RM,P1)==(2*res0)_(RP,P1))     -   (9) is_inexact_It_midpoint0=!is_exact0 && !is_midpoint0 &&         (res1_RN==res1_RM)     -   (10) is_inexact_gt_midpoint0=!is_exact0 && !is_midpoint0 &&         (res1 _RN==res1_RP)     -   (11) Repeat steps (1) through (10) for (1)         res2=(res1)_(RA,P2), (2) res2_RN2=(res1)_(RN,P2) (3)         res2_RM=(res1)_(RM,P2), (4) res2RP=(res1)_(RP,P2), (5) (2*res1)         _(RM,P2) and (6) (2*res1)_(RP,P2) in order to determine (7)         is_exact1, (8) is_midpoint1, (9) is_inexact_It_midpoint1,         and (10) is_inexact_gt_midpoint1.

The methods of FIG. 2 may enable compliance with the requirements of the IEEE draft of standard 754 with relatively little effort. The hardware embodiment of FIG. 4 requires only a minor addition to the hardware used to perform floating point arithmetic. A possible software implementation of the method above (that for binary floating-point uses existing operations implemented in hardware) can be based on calculating the rounding indicators listed above using different rounding modes (to nearest and directed) as follows, and also using existing hardware for very good performance.

In contrast, other ways of complying with the new IEEE 754 operations may prove very burdensome. Double rounding errors are currently ignored in most cases, as they are viewed as ordinary and inevitable rounding errors for floating-point operations (at the present time this is the case for binary floating-point operations using the rounding to nearest-even mode; decimal floating-point operations and the rounding to nearest-away mode are new additions in the revised IEEE Standard 754R).

Building new hardware to directly perform the new IEEE 754 operations may prove very burdensome. If only homogeneous operations (with operands and results in the same format) and conversion operations between various floating-point formats are implemented, then mixed-format operations could not be performed without the risk of double rounding errors. For example, this would be the case for decimal floating-point operations of type ‘decimal128+decimal128=decimal64’. The straightforward method would be to implement a dedicated IEEE-correct operation for this either in hardware, or in software. A dedicated hardware implementation would be difficult to justify because ‘decimal128+decimal128=decimal64’ operations will most likely be a lot less frequent than ‘decimal128+decimal128=decimal128” or ‘decimal64+decimal64=decimal64’ (assuming that these already have hardware implementations). In addition, mixed-format operation types would be quite numerous when several floating-point formats are supported. The same observation applies if the homogeneous operations are implemented in software.

Moreover, the revised standard IEEE 754R mandates that all possible combinations of supported formats for operands and results have to be implemented in order to be compliant (i.e. the results have to be correctly rounded). For decimal floating-point, the rounding to nearest-away mode has to be supported (along with four other rounding modes). For example, combining just values in decimal64 and decimal 128 format for decimal floating-point addition, for compliance we will have to implement operations for:

decimal64+decimal64=decimal64   1)

-   -   (already exists in current IEEE 754R software implementation)

decimal64+decimal128=decimal64   2)

-   -   (double rounding errors are possible; can use the method         presented here)

decimal128+decimal64=decimal64   3)

-   -   (double rounding errors are possible; can use the method         presented here)

decimal128+decimal128=decimal64   4)

-   -   (double rounding errors are possible; can use the method         presented here)

decimal64+decimal64=decimal128   5)

-   -   (promote operands to decimal128 and use case 8); no double         rounding errors)

decimal64+decimal128=decimal128   6)

-   -   (promote operands to decimal 128 and use case 8); no double         rounding errors)

decimal128+decimal64=decimal128   7)

-   -   (promote operands to decimal 128 and use case 8); no double         rounding errors)

decimal128+decimal128=decimal128   8)

-   -   (already exists in current IEEE 754R software implementation)         Implementing all of these cases separately may be very         burdensome.

Turning to FIG. 3, depicted is an arithmetic module 300 to correct double rounding errors when rounding to nearest away. Arithmetic module 300 includes floating point module 310 and corrector 340. Floating point module 310 may perform arithmetic operations and round the results. Corrector 340 may correct the results for double rounding errors.

Floating point module 310 includes operation performer 320 and rounder 330. Operation performer may perform floating point operations. The operations may require rounding, because the intermediate results may produce more digits than permitted by the specified precision. Rounder 330 performs rounding, either to the intermediate results produced by operation performer 320 or when an explicit request for rounding is made. For example, a command may convert a floating point number to a lesser precision. The operations of floating point module 310 may produce errors from double rounding. For example, an arithmetic operation which involves rounding may be followed by a conversion to lower precision.

Corrector 340 corrects double rounding errors. It includes double rounding detector 345, adjuster 350, and designator 355. Double rounding detector 345 detects double rounding errors. Double rounding detector 345 may use the indicators described above. In particular, it may check that the second number to be rounded is at midpoint and the first number to be rounded was rounded up. Adjuster 350 corrects double-rounding errors when they are detected by detector 345. Adjuster 350 may correct the errors by subtracting one from the significand of the result. If the significand is below the specified number of digits indicated by the precision, adjuster 350 may change the result as described in connection with elements 255 and 260 of flowchart 200 in FIG. 2. Designator 355 may change the values of the rounding indicators in case a double rounding occurred, but no double rounding error. Designator 355 may follow the method described in connection with steps 265, 270, 275, and 280 of flowchart 200 of FIG. 2

FIG. 3 is for illustration and not limitation. In hardware embodiments, a floating point module and a corrector may be combined into a single hardware module. In software embodiments of a corrector, the corrector and the floating point module may not be components of the same module.

Various embodiments of the disclosed subject matter may be implemented in hardware, firmware, software, or combination thereof, and may be described by reference to or in conjunction with program code, such as instructions, functions, procedures, data structures, logic, application programs, design representations or formats for simulation, emulation, and fabrication of a design, which when accessed by a machine results in the machine performing tasks, defining abstract data types or low-level hardware contexts, or producing a result.

For simulations, program code may represent hardware using a hardware description language or another functional description language which essentially provides a model of how designed hardware is expected to perform. Program code may be assembly or machine language, or data that may be compiled and/or interpreted. Furthermore, it is common in the art to speak of software, in one form or another as taking an action or causing a result. Such expressions are merely a shorthand way of stating execution of program code by a processing system which causes a processor to perform an action or produce a result.

Program code may be stored in, for example, volatile and/or non-volatile memory, such as storage devices and/or an associated machine readable or machine accessible medium including solid-state memory, hard-drives, floppy-disks, optical storage, tapes, flash memory, memory sticks, digital video disks, digital versatile discs (DVDs), etc., as well as more exotic mediums such as machine-accessible biological state preserving storage. A machine readable medium may include any mechanism for storing, transmitting, or receiving information in a form readable by a machine, and the medium may include a tangible medium through which electrical, optical, acoustical or other form of propagated signals or carrier wave encoding the program code may pass, such as antennas, optical fibers, communications interfaces, etc., including wireless access mechanisms. Program code may be transmitted in the form of packets, serial data, parallel data, propagated signals, etc., and may be used in a compressed or encrypted format.

Program code may be implemented in programs executing on programmable machines such as mobile or stationary computers, personal digital assistants, set top boxes, cellular telephones and pagers, and other electronic devices, each including a processor, volatile and/or non-volatile memory readable by the processor, at least one input device and/or one or more output devices. Program code may be applied to the data entered using the input device to perform the described embodiments and to generate output information. The output information may be applied to one or more output devices. One of ordinary skill in the art may appreciate that embodiments of the disclosed subject matter can be practiced with various computer system configurations, including multiprocessor or multiple-core processor systems, minicomputers, mainframe computers, as well as pervasive or miniature computers or processors that may be embedded into virtually any device. Embodiments of the disclosed subject matter can also be practiced in distributed computing environments where tasks may be performed by remote processing devices that are linked through a communications network.

It will be apparent to those skilled in the art having the benefit of this disclosure that the present invention contemplates methods and arrangements for correcting for double rounding errors when rounding floating point numbers to nearest away. It is understood that the form of the invention shown and described in the detailed description and the drawings are to be taken merely as examples. It is intended that the following claims be interpreted broadly to embrace all the variations of the example embodiments disclosed.

Although the present invention and some of its advantages have been described in detail for some embodiments, it should be understood that various changes, substitutions and alterations can be made herein without departing from the spirit and scope of the invention as defined by the appended claims. Although an embodiment of the invention may achieve multiple objectives, not every embodiment falling within the scope of the attached claims will achieve every objective. Moreover, the scope of the present application is not intended to be limited to the particular embodiments of the process, machine, manufacture, composition of matter, means, methods and steps described in the specification. As one of ordinary skill in the art will readily appreciate from the disclosure of the present invention, processes, machines, manufacture, compositions of matter, means, methods, or steps, presently existing or later to be developed that perform substantially the same function or achieve substantially the same result as the corresponding embodiments described herein may be utilized according to the present invention. Accordingly, the appended claims are intended to include within their scope such processes, machines, manufacture, compositions of matter, means, methods, or steps. 

1. A method of floating point arithmetic on a computing device, the method comprising: performing a floating point operation on one or more floating point numbers of precision P1 in base b, the performing comprising: performing the floating point operation to obtain a positive intermediate result res0 of precision greater than P1; and rounding positive result res0 to precision P1 to the nearest away, thereby producing positive result res1 of the floating point operation; rounding the result res1 to precision P2 to the nearest away, where precision P2 is narrower than precision P1, thereby producing result res2; and correcting res2 for double rounding errors, the correcting comprising: determining that res1 is midway between two consecutive floating point numbers of precision P2, the larger being res2; determining that rounding res0 to produce res1 involved rounding up; and decrementing the significand of res2 to obtain the corrected result res2′.
 2. The method of claim 1, further comprising: determining that the significand of res2′ is equal to b^((P2−1))−1; setting the significand of the corrected result res2′ equal to b^(P2)−1; and decrementing the unbiased exponent of res2′.
 3. The method of claim 1, further comprising: performing a second floating point operation on one or more floating point numbers of precision P1 in base b, thereby producing positive result res_(1,2); rounding the result res_(1,2) to precision P2 to the nearest away, where P2 is narrower than P1, thereby producing result res_(2,2); determining that res_(1,2) is midway between two consecutive floating point numbers of precision P2, the larger being res_(2,2); determining that performing the floating point operation to precision P1 to produce result res_(1,2) included rounding down to the nearest away; setting a rounding indicator for res_(2,2) to indicate that res_(2,2) is past midway between two consecutive floating point numbers of precision P2, performing a third floating point operation on one or more floating point numbers of precision P1 in base b, thereby producing positive result res_(1,3) rounding the result res_(1,3) to precision P2 to the nearest away, where P2 is narrower than P1, thereby producing result res_(2,3), wherein the rounding is exact; determining that performing the floating point operation to precision P1 to produce result res_(1,3) included rounding to the nearest away; and setting a rounding indicator for res_(2,3) to indicate that generating res_(2,3) included rounding.
 4. The method of claim 1, wherein: the correcting comprises: calculating a rounding digit r0 for res0 and a rounding digit r1 for res1, wherein the rounding digit for a floating point number is the digit to the right of the least significant digit that fills in the significand of the floating point number; and calculating a sticky bit s0 for res0 and a sticky bit s1 for res1, wherein the sticky bit for a floating point number is the logical OR of all of the digits to the right of the rounding digit of the significand of the floating point number; the determining that res1 is midway comprises determining that r1=b/2 and that s1 is false; and the determining that rounding res0 to produce res1 involved rounding up comprises determining that r0≧b/2.
 5. The method of claim 4, wherein calculating r0, calculating r1, calculating s0, and calculating s1 comprise calculating r0, r1, s0, and s1 in hardware.
 6. The method of claim 1, wherein the correcting comprises: calculating res0 rounded to nearest-even; calculating res0 rounded to negative infinity; calculating res0 rounded to positive infinity; calculating 2×res0 rounded to negative infinity; and calculating 2×res0 rounded to positive infinity.
 7. The method of claim 6, wherein the calculating res0 rounded to nearest-away; the calculating res0 rounded to nearest-even, to negative infinity and to positive infinity; and the calculating 2×res0 rounded to negative infinity and rounded to positive infinity comprise calculating in software.
 8. An apparatus for floating point arithmetic on a computing device, the apparatus comprising: a floating point module to: perform a floating point operation on one or more floating point numbers of precision P1 in base b, the performing comprising: performing the floating point operation to obtain a positive intermediate result res0 of precision greater than P1; and rounding positive result res0 to precision P1 to the nearest away, thereby producing positive result res1 of the floating point operation; and round the result res1 to precision P2 to the nearest away, where precision P2 is narrower than precision P1, thereby producing result res2; and a corrector to correct res2 for double rounding errors, the corrector to: determine that res1 is midway between two consecutive floating point numbers of precision P2, the larger being res2; determine that rounding res0 to produce res1 involved rounding up; and decrement the significand of res2 to obtain the corrected result res2′.
 9. The apparatus of claim 8, wherein the corrector is to: determine that the significand of res2′ is equal to b^((P2−1))−1; set the significand of the corrected result res2′ equal to b^(P2)−1; and decrement the unbiased exponent of res2′.
 10. The apparatus of claim 8, further comprising a designator to: perform a second floating point operation on one or more floating point numbers of precision P1 in base b, thereby producing positive result res_(1,2); round the result res_(1,2) to precision P2 to the nearest away, where P2 is narrower than P1, thereby producing result res_(2,2); determine that res_(2,2) is midway between two consecutive floating point numbers of precision P2, the larger being res_(2,2); determine that performing the floating point operation to precision P1 to produce result res_(1,2) included rounding down to the nearest away; set a rounding indicator for res_(2,2) to indicate that res_(2,2) is past midway between two consecutive floating point numbers of precision P2, perform a third floating point operation on one or more floating point numbers of precision P1 in base b, thereby producing positive result res_(1,3) round the result res_(1,3) to precision P2 to the nearest away, where P2 is narrower than P1, thereby producing result res_(2,3), wherein the rounding is exact; determine that performing the floating point operation to precision P1 to produce result res_(1,3) included rounding to the nearest away; and set a rounding indicator for res_(2,3) to indicate that generating res_(2,3) included rounding.
 11. The apparatus of claim 8, wherein the corrector comprises software.
 12. The apparatus of claim 8, wherein the corrector is to: calculate a rounding digit r0 for res0 and a rounding digit r1 for res1, wherein the rounding digit for a floating point number is the digit to the right of the least significant digit that fills in the significand of the floating point number; and calculate a sticky bit s0 for res0 and a sticky bit s1 for res1, wherein the sticky bit for a floating point number is the logical OR of all of the digits to the right of the rounding digit of the significand of the floating point number.
 13. The apparatus of claim 12, wherein the corrector comprises hardware.
 14. A machine-accessible medium, the medium having one or more associated instructions to perform floating point arithmetic, wherein the one or more instructions, when executed, cause a machine to: perform a floating point operation on one or more floating point numbers of precision P1 in base b, the performing comprising: performing the floating point operation to obtain a positive intermediate result res0 of precision greater than P1; and rounding positive result res0 to precision P1 to the nearest away, thereby producing positive result res1 of the floating point operation; round the result res0 to precision P2 to the nearest away, where precision P2 is narrower than precision P1, thereby producing result res2; and correct res2 for double rounding errors, the correcting comprising: determining that res1 is midway between two consecutive floating point numbers of precision P2, the larger being res2; determining that rounding res0 to produce res1 involved rounding up; and decrementing the significand of res2 to obtain the corrected result res2′.
 15. The medium of claim 14, wherein the one or more associated instructions, when executed, further cause the machine to: determine that the significand of res2′ is equal to b^((P2−1))−1; set the significand of the corrected result res2′ equal to b^(P2)−1; and decrement the unbiased exponent of res2′.
 16. The medium of claim 14, wherein the one or more associated instructions, when executed, further cause the machine to: perform a second floating point operation on one or more floating point numbers of precision P1 in base b, thereby producing positive result res_(1,2); round the result res_(1,2) to precision P2 to the nearest away, where P2 is narrower than P1, thereby producing result res_(2,2); determine that res_(1,2) is midway between two consecutive floating point numbers of precision P2, the larger being res_(2,2); determine that performing the floating point operation to precision P1 to produce result res_(1,2) included rounding down to the nearest away; set a rounding indicator for res_(2,2) to indicate that res_(2,2) is past midway between two consecutive floating point numbers of precision P2, perform a third floating point operation on one or more floating point numbers of precision P1 in base b, thereby producing positive result res_(1,3) round the result res_(1,3) to precision P2 to the nearest away, where P2 is narrower than P1, thereby producing result res_(2,3), wherein the rounding is exact; determine that performing the floating point operation to precision P1 to produce result res_(1,3) included rounding to the nearest away; and set a rounding indicator for res_(2,3) to indicate that generating res_(2,3) included rounding.
 17. The medium of claim 14, wherein: the one or more instructions which cause the machine to correct comprise one or more instructions which cause the machine to: calculate a rounding digit r0 for res0 and a rounding digit r1 for res1, wherein the rounding digit for a floating point number is the digit to the right of the least significant digit that fills in the significand of the floating point number; and calculate a sticky bit s0 for res0 and a sticky bit s1 for res1, wherein the sticky bit for a floating point number is the logical OR of all of the digits to the right of the rounding digit of the significand of the floating point number; the one or more instructions which cause the machine to determine that res1 is midway comprise one or more instructions which cause the machine to determine that r1=b/2 and that s1 is false; and the one or more instructions which cause the machine to determine that rounding res0 to produce res1 involved rounding up comprise one or more instructions which cause the machine to determine that r0≧b/2.
 18. The medium of claim 17, wherein the one or more instructions which cause the machine to calculate r0, calculate r1, calculate s0, and calculate s1 comprise one or more instructions which cause the machine to calculate r0, r1, s0, and s1 in hardware.
 19. The medium of claim 14, wherein the wherein the one or more instructions which cause the machine to correct comprise one or more instructions which cause the machine to: calculate res0 rounded to nearest-even; calculate res0 rounded to negative infinity; calculate res0 rounded to positive infinity; calculate 2×res0 rounded to negative infinity; and calculate 2×res0 rounded to positive infinity.
 20. The medium of claim 19, wherein the one or more instructions which cause the machine to calculate res0 rounded to nearest-away; calculate res0 rounded to nearest-even, to negative infinity and to positive infinity; and calculate 2×res0 rounded to negative infinity and rounded to positive infinity comprise one or more instructions which cause the machine to calculate in software. 